﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Foundation {
    /// <summary>
    /// Über diese Klasse werden Effekte definiert. 
    /// </summary>
    [DataContract]
    public class Effect : IEquatable<Effect> {
        /// <summary>
        /// private Propery für die ID des Effektes
        /// </summary>
        private Guid id;

        /// <summary>
        /// private Property für das Volume des Effektes
        /// </summary>
        private int volume;

        /// <summary>
        /// Name des Effektes
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// Key des Effektes
        /// </summary>
        [DataMember]
        public char Key { get; set; }

        /// <summary>
        /// Die Art des Effektes
        /// </summary>
        [DataMember]
        public EffectTypeEnum Type { get; set; }

        /// <summary>
        /// Eine Liste, welche alle Tracks, welche zum Effekt gehören, beinhaltet
        /// </summary>
        [DataMember]
        public List<Track> Tracks { get; set; }

        /// <summary>
        /// Die Lautstärke, in welcher der Effekt abgespielt werden soll
        /// </summary>
        [DataMember]
        public int Volume {
            get {
                return this.volume;

            }
            set {
                if (value > 100) {
                    this.volume = 100;
                }
                else if (value < 0) {
                    this.volume = 0;
                }
                else {
                    this.volume = value;
                }
            }
        }

        /// <summary>
        /// Die ID des Effektes
        /// </summary>
        [DataMember]
        public Guid Id {
            get {
                if (this.id == Guid.Empty) {
                    this.id = Guid.NewGuid();
                }
                return this.id;
            }
            set {
                this.id = value;
            }
        }

        /// <summary>
        /// Die ID der übergeordneten EffectGroup
        /// </summary>
        [DataMember]
        public Guid ParentId { get; set; }

        /// <summary>
        /// Konstruktor
        /// </summary>
        public Effect() {
            this.Tracks = new List<Track>();
        }

        /// <summary>
        /// Gibt die String Representation des Objektes zurück
        /// </summary>
        /// <returns>String Representation</returns>
        public override string ToString() {
            if (string.IsNullOrEmpty(Name) && Key.Equals('\0')) {
                return "Unbekannter Effekt";
            }
            if (Key.Equals('\0')) {
                return Name;
            }
            if (string.IsNullOrEmpty(Name)) {
                return Key.ToString();
            }
            return string.Format("{0} - {1}", Key, Name);
        }

        /// <summary>
        /// Vergleicht ein mitgegebenes Objekt und gibt zurück, ob es sich um das selbe Objekt handelt
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(Effect other) {
            return this.id.Equals(other.Id);
        }
    }
}
